home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / net / bind-contrib.tar.gz / bind-contrib.tar / contrib / misc / dnsfind.shar / find-mx < prev    next >
Encoding:
Text File  |  1996-10-25  |  2.8 KB  |  145 lines

  1. #!/usr/local/bin/perl
  2. # find-mx -    Find all names which list a host as "best-MX"
  3. #
  4. # $Id: dnsfind.shar,v 8.2 1996/10/25 17:07:55 vixie Exp $
  5. # $Source: /proj/src/isc/cvs-1/bind/contrib/misc/dnsfind.shar,v $
  6. #
  7. # SYNOPSIS
  8. #    find-mx hub domain [...]
  9. #
  10.  
  11. push (@INC, '/src/util/perl');
  12. require 'dnsfind.pl';
  13.  
  14. $Prog = "find-mx";
  15.  
  16. if ($#ARGV <= 0) {
  17.     die "usage: $Prog hub domain [...]\n";
  18. }
  19.  
  20. if (! &main (@ARGV)) {
  21.     exit 1;
  22. }
  23.  
  24.  
  25. #############################################################################
  26. # Main Program
  27. #
  28. sub main {
  29.     local ($hub) = shift (@_);
  30.     local (@domains) = @_;
  31.  
  32.     &dnsfind (@domains);
  33.  
  34.     undef %mx;
  35.     foreach $zone (keys (%Exchangers)) {
  36.     @exchangers = split ('\|', $Exchangers{$zone});
  37.  
  38.     #
  39.     # If hub is one of the most preferred MX for this zone, then
  40.     # save it
  41.     #
  42.     if (grep ($_ eq $hub, @exchangers)) {
  43.         $mx{$zone} = 1;
  44.     }
  45.     }
  46.  
  47.     #
  48.     # Check if any CNAMEs point to an MX we have selected
  49.     #
  50.     while (1) {
  51.     ($key, $value) = each (%Cnames);
  52.     last if (! length ($key));
  53.  
  54.     if ($mx{$value}) {
  55.         $mx{$key} = 1;
  56.     }
  57.     }
  58.     
  59.  
  60.     #
  61.     # Custom sort our output
  62.     #
  63.     @mx = sort (custom_sort keys (%mx));
  64.  
  65.     #
  66.     # Print it
  67.     #
  68.     foreach $zone (@mx) {
  69.     print ($zone, "\n");
  70.     }
  71.  
  72.     return 1;
  73. }
  74.  
  75.  
  76. #############################################################################
  77. # dnswanted -    Called for each record found by dnsfind
  78. #
  79. sub dnswanted {
  80.     local ($pref, $exchanger);
  81.     local ($zone_c);
  82.  
  83.     $type =~ tr/[a-z]/[A-Z]/;
  84.  
  85.     if ($type eq 'MX') {
  86.     local ($pref);
  87.     local ($exchanger);
  88.  
  89.     ($pref, $exchanger) = split ('\s+', $value);
  90.     $zone = &cleanhost ($zone);
  91.     $exchanger = &cleanhost ($exchanger);
  92.  
  93.     if (defined ($Lowest_pref{$zone})) {
  94.         if ($pref == $Lowest_pref{$zone}) {
  95.         $Exchangers{$zone} = $Exchangers{$zone} . '|' .
  96.             $exchanger;
  97.         } else {
  98.         if ($pref < $Lowest_pref{$zone}) {
  99.             $Lowest_pref{$zone} = $pref;
  100.             $Exchangers{$zone} = $exchanger;
  101.         }
  102.         }
  103.     } else {
  104.         $Lowest_pref{$zone} = $pref;
  105.         $Exchangers{$zone} = $exchanger;
  106.     }
  107.     } elsif ($type eq 'CNAME') {
  108.         #
  109.         # Save this, in case it points to a selected MX
  110.         #
  111.         $zone_c = &cleanhost ($zone);
  112.         $Cnames{$zone_c} = &cleanhost ($value);
  113.     }
  114. }
  115.  
  116.  
  117. #############################################################################
  118. # custom_sort -    Sorts domain names by number of parts in name then alphanum
  119. #
  120. sub custom_sort {
  121.     local ($dots_a);
  122.     local ($dots_b);
  123.     local ($cmp);
  124.  
  125.     $dots_a = $a; $dots_a =~ tr/.//dc;
  126.     $dots_b = $b; $dots_b =~ tr/.//dc;
  127.  
  128.     $cmp = $dots_a cmp $dots_b;
  129.     if ($cmp == 0) {
  130.     $cmp = $a cmp $b;
  131.     }
  132.     return $cmp;
  133. }
  134.  
  135. #############################################################################
  136. # cleanhost -    Fold case to lower and remove trailing dot if present
  137. #
  138. sub cleanhost {
  139.     local ($host) = shift (@_);
  140.  
  141.     $host =~ tr/[A-Z]/[a-z]/;
  142.     $host =~ s/\.$//;
  143.     return ($host);
  144. }
  145.